﻿using MediatR;
using Tb.App.API.Applications.Command.Basic;
using Tb.App.API.Dtos.Rabbit;
using Tb.App.Domain.Rabbit.Basic;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Basic
{
    /// <summary>
    /// 溯源商品基本信息反填-命令
    /// </summary>
    public class GetFanBasicQueryCommandHandler : IRequestHandler<GetFanBasicQueryCommand, ApiResult<BasicDTO>>
    {
        private readonly IBaseRepository<BasicModel> basicRep;
        private readonly IBaseRepository<StoreModel> storeRep;

        public GetFanBasicQueryCommandHandler(IBaseRepository<BasicModel> basicRep,
            IBaseRepository<StoreModel> storeRep)
        {
            this.basicRep = basicRep;
            this.storeRep = storeRep;
        }
        public Task<ApiResult<BasicDTO>> Handle(GetFanBasicQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<BasicDTO> result = new ApiResult<BasicDTO>();
            var list = from a in basicRep.GetAll().Where(a => !a.IsDel&&a.Batch==request.Batch)
                       join b in storeRep.GetAll().Where(b => !b.IsDel)
                       on a.Sid equals b.Id
                       select new BasicDTO
                       {
                           Id = a.Id,
                           Sid = a.Sid,
                           StoreName = b.StoreName,
                           Batch = a.Batch,
                           Expiration = a.Expiration,
                           License = a.License,
                           Company = a.Company,
                           Picture = a.Picture,
                           Completeness = a.Completeness,
                           IsDel = a.IsDel,
                           Creator = a.Creator,
                           CreatorDate = a.CreatorDate,
                           Updator = a.Updator,
                           UpdatorDate = a.UpdatorDate,
                       };
            var entity=list.FirstOrDefault();
            result.Code = ApiEnum.查询成功;
            result.Message = "反填溯源商品基本信息成功";
            result.Data = entity;

            return Task.FromResult(result);
        }
    }
}
